package cn.seapon.talker;

import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.seapon.talker.common.MessageContent;
import cn.seapon.talker.common.MessageGenre;
import cn.seapon.talker.common.TMessage;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.net.SocketUtils;

public class SendFriendRequestActivity extends Activity {

	private Bundle prevExtra;

	private EditText et_request_message;
	private Button send_btn;

	private RequestResultBroadcastReceiver receiver;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(SendFriendRequestActivity.this, "请求发送成功",
						Toast.LENGTH_SHORT).show();
				break;

			case -1:
				Toast.makeText(SendFriendRequestActivity.this, "请求发送失败",
						Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}

	};
	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.send_btn:

				new Thread() {

					@Override
					public void run() {
						Message message = Message.obtain();
						String text = et_request_message.getText().toString()
								.trim();
						String userid = prevExtra.getString("userid");
						JSONObject object = new JSONObject();
						try {
							object.put("userid", userid);
							object.put("text", text);
							object.put("senderInfo",
									SharedItems.userInformation.getJSONString());
							TMessage tMessage = new TMessage();
							tMessage.setGenre(MessageGenre.ADD_FRIEND);
							tMessage.setSendTime(System.currentTimeMillis());
							tMessage.setSenderId(SharedItems.userInformation
									.getAccount());
							MessageContent content = new MessageContent();
							content.setOthers(object.toString());
							tMessage.setContent(content);
							if (SocketUtils.writeStringToSocket(
									SharedItems.socket,
									tMessage.getJSonString())) {
								message.what = 1;
							} else {
								message.what = -1;
							}
							handler.sendMessage(message);
						} catch (Exception e) {
							e.printStackTrace();
							return;
						}
					}

				}.start();

				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_friend_request);
		et_request_message = (EditText) findViewById(R.id.et_value);
		send_btn = (Button) findViewById(R.id.send_btn);

		prevExtra = getIntent().getExtras();
		receiver = new RequestResultBroadcastReceiver();

		send_btn.setOnClickListener(listener);
	}

	@Override
	protected void onStart() {
		registerReceiver(receiver, new IntentFilter(
				"cn.seapon.talker.FRIEND_ADD_RESULT"));
		super.onStart();
	}

	@Override
	protected void onStop() {
		unregisterReceiver(receiver);
		super.onStop();
	}

	public class RequestResultBroadcastReceiver extends BroadcastReceiver {
		private static final String TAG = "||__RequestResultBroadcastReceiver__||";
		private Context context;

		public RequestResultBroadcastReceiver() {
		}

		public RequestResultBroadcastReceiver(Context context) {
			this.context = context;
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			boolean resultCode = bundle.getBoolean("resultCode");
			if (resultCode) {
				Toast.makeText(SendFriendRequestActivity.this,
						"系统成功接收你的请求，正等待对方响应！", Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(SendFriendRequestActivity.this,
						"系统正在维护，请稍后发送请求！", Toast.LENGTH_SHORT).show();
			}
		}
	}

}
